package com.mvp.mvpdemo.mvp.presenter

import com.mvp.mvpdemo.app.App
import com.mvp.mvpdemo.base.BasePresenter
import com.mvp.mvpdemo.bean.LoginBean
import com.mvp.mvpdemo.bean.UpLoadFile
import com.mvp.mvpdemo.mvp.contract.LoginContract
import com.mvp.mvpdemo.mvp.model.LoginModel
import com.retrofit.retrofitutil.RetrofitCallback
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.Dispatcher
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.io.File


/**
 * author    : mahongyin
 * e-mail    : mhy.work@foxmail.com
 * date      : 2020-10-17 11:09
 * introduce :
 */
class LoginPresenter : BasePresenter<LoginContract.Model, LoginContract.View>(),
    LoginContract.Presenter {

    override fun createModel(): LoginContract.Model = LoginModel()

    /**
     * 使用协程
     */
    override  fun login(phone: String, pwd: String) {
        showLoading()
          coroutineScope.launch {
            try {
                val model = mModel?.loginCC(phone, pwd)
                model?.let { getView()?.loginSusscess(it) }
                // 请求成功，
            } catch (e: Exception) {
                // 请求失败或取消
                getView()?.error(e.message + "")
                e.printStackTrace()
            } finally {
                hideLoading()
            }
        }
    }

    override fun logout() {

        showLoading()
        val call: Call<LoginBean>? = mModel?.logout()
        call?.enqueue(object : RetrofitCallback<LoginBean?>() {
            override fun onSuccess(model: LoginBean?) {
                model?.let { getView()?.logoutSusscess(it) }
            }

            override fun onFailure(code: Int, msg: String?) {
                getView()?.error(msg + "")
            }

            override fun onThrowable(t: Throwable) {
                getView()?.error(t.message + "")
            }

            override fun onFinish() {
                hideLoading()
            }
        })


    }

    override fun upLaodFile(file: File) {
        showLoading()
        mModel?.upLoadFile(file)?.enqueue(object : Callback<UpLoadFile?> {

            override fun onFailure(call: Call<UpLoadFile?>, t: Throwable) {
                hideLoading()
                getView()?.error(t.message + "")
            }

            override fun onResponse(call: Call<UpLoadFile?>, response: Response<UpLoadFile?>) {
                hideLoading()
                if (response.isSuccessful)
                    getView()?.showLongToast(response.body().toString())
                else
                    getView()?.error(response.errorBody().toString() + "")
            }
        })
    }

    override fun upLaodFiles(user: String, token: String, file: File) {
        mModel?.upLoadFiles(user, token, file)?.enqueue(object : Callback<UpLoadFile?> {

            override fun onFailure(call: Call<UpLoadFile?>, t: Throwable) {
                hideLoading()
                getView()?.error(t.message + "")
            }

            override fun onResponse(call: Call<UpLoadFile?>, response: Response<UpLoadFile?>) {
                hideLoading()
                if (response.isSuccessful)
                    getView()?.showLongToast(response.body().toString())
                else
                    getView()?.error(response.errorBody().toString() + "")
            }
        })

    }


}